﻿Imports System.Data.Linq
Imports System.Linq

Public Class MOilUnitPrices
    Private db As New TanNamChinhDataContext
    Public Function getUnitPricesList1() As Table(Of tblOilUnitPrice)
        Try
            Return db.tblOilUnitPrices
        Catch ex As Exception
            Throw ex
        End Try
    End Function
    Public Function getUnitPricesList2() As IQueryable(Of tblOilUnitPrice)
        Try
            Dim query = From b In db.tblOilUnitPrices
                        Select b
            'Return (From r In db.tblOilUnitPrices
            '           Select r).Single
            Return query
        Catch ex As Exception
            Throw ex
        End Try
    End Function
    Public Function getPriceCurrent(ByVal dateCurrent As DateTime) As Integer
        Dim price As Integer
        Try
            'price = (From b In db.tblOilUnitPrices
            '         Where b.DateChange <= dateCurrent
            '         Order By b.DateChange Descending
            '            Select b.UnitPrice
            '           ).First()
            price = (From b In db.tblOilUnitPrices
                    Order By b.DateChange Descending
                       Select b.UnitPrice
                      ).First()
        Catch ex As Exception
            Throw ex
        End Try
        Return price
    End Function
    Public Sub InsertNewRecord(ByVal obj As tblOilUnitPrice)
        'db.tblOilUnitPrices.InsertOnSubmit(obj)
        db.GetTable(Of tblOilUnitPrice)().InsertOnSubmit(obj)
        db.SubmitChanges()
    End Sub
    Public Sub UpdateRecord(ByVal obj As tblOilUnitPrice)
        Dim getData = (From b In db.tblOilUnitPrices
                                Where b.ID = obj.ID
                                Select b).Single()
        getData.UnitPrice = obj.UnitPrice
        getData.DateChange = obj.DateChange
        getData.Description = obj.Description
        db.SubmitChanges()

    End Sub
    Public Sub DeleteRecord(ByVal id As Integer)
        Dim getData = (From b In db.tblOilUnitPrices
                                Where b.ID = id
                                Select b).Single()
        db.tblOilUnitPrices.DeleteOnSubmit(getData)
        db.SubmitChanges()
    End Sub
    Public Function getNewestPriceFile() As Integer
        Dim prices As Integer
        Try
            prices = (From b In db.tblOilUnitPrices
                     Order By b.ID Descending
                        Select b.UnitPrice
                       ).First()
        Catch ex As Exception
            Return 0
        End Try
        Return prices
    End Function
End Class
